package com.repair.controller;


import cn.hutool.core.bean.BeanUtil;
import com.repair.dto.RepairStoreDto;
import com.repair.entity.RepairStore;
import com.repair.form.*;
import com.repair.param.RepairStoreAddParam;
import com.repair.param.RepairStoreFindParam;
import com.repair.param.RepairStoreStateParam;
import com.repair.param.RepairStoreUpdateParam;
import com.repair.service.RepairStoreService;
import com.repair.util.PageInfo;
import com.repair.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 维修材料库存表 前端控制器
 * </p>
 *
 * @author 小叶
 * @since 2024年04月03日
 */
@RestController
@RequestMapping("/repairStore")
@Api(tags = "维修材料库存相关接口")
public class RepairStoreController {
    @Autowired
    private RepairStoreService repairStoreService;

    /**
     * 新增维修材料
     * @param form 前端需要传递的维修材料信息，包含：材料名称，材料规格，材料数量，材料单价，备注
     * @return 返回新增的材料信息
     */
    @PostMapping("/add")
    @ApiOperation("新增维修材料")
    public Result add(@RequestBody RepairStoreAddForm form){
        RepairStore repairStore= repairStoreService.add(BeanUtil.toBean(form, RepairStoreAddParam.class));
        return Result.success("新增维修材料成功",repairStore);
    }

    /**
     * 修改维修材料信息
     * @param form 修改的内容，包含：id，材料数量，备注，材料单价
     * @return 返回修改后的维修材料信息
     * @throws InterruptedException
     */
    @PostMapping("/update")
    @ApiOperation("修改维修材料信息")
    public Result update(@RequestBody RepairStoreUpdateForm form) throws InterruptedException {
        RepairStore repairStore =  repairStoreService.updateRepairStore(BeanUtil.toBean(form, RepairStoreUpdateParam.class));
        return Result.success("修改维修材料信息成功",repairStore);
    }

    /**
     * 修改维修材料状态
     * @param form 要修改的数据，包含id，修改后的材料状态
     * @return 返回修改后的维修材料信息
     */
    @PostMapping("/changeState")
    @ApiOperation("修改维修材料状态")
    public Result changeState(@RequestBody RepairStoreStateForm form){
        RepairStore repairStore =  repairStoreService.changeState(BeanUtil.toBean(form, RepairStoreStateParam.class));
        return  Result.success("修改维修材料状态成功",repairStore);
    }

    /**
     * 根据id查询维修材料信息
     * @param id 要查询的维修材料id
     * @return  返回查询结果
     */
    @GetMapping("/findById")
    @ApiOperation("根据id查询维修材料信息")
    public Result findById(Integer id){
        RepairStore repairStore= repairStoreService.findById(id);
        return Result.success("id查询维修材料信息成功",repairStore);
    }

    /**
     * 维修员查询材料信息(上架的材料)
     * @param form 查询条件，包含：材料名称，材料规格，材料状态，页码，每页数量
     * @return 返回的查询结果
     */
    @GetMapping("/repairerFind")
    @ApiOperation("维修员条件查询材料信息")
    public Result repairerFind(RepairStoreFindForm form){
        RepairStoreFindParam param = BeanUtil.toBean(form, RepairStoreFindParam.class);
        param.setIsAdmin(0);
        PageInfo<RepairStoreDto> pageInfo =  repairStoreService.repairFind(param);
        return Result.success("维修员条件查询材料信息成功",pageInfo);
    }

    /**
     * 管理员查询材料信息
     * @param form 查询条件，包含：材料名称，材料规格，材料状态，页码，每页数量
     * @return 返回的查询结果
     */
    @GetMapping("/adminFind")
    @ApiOperation("管理员条件查询材料信息")
    public Result adminFind(RepairStoreFindForm form){
        RepairStoreFindParam param = BeanUtil.toBean(form, RepairStoreFindParam.class);
        param.setIsAdmin(1);
        PageInfo<RepairStoreDto> pageInfo =  repairStoreService.repairFind(param);
        return Result.success("管理员条件查询材料信息成功",pageInfo);
    }

}

